/**
 * Contains functions to display and modify an embedded Google Map.
 *
 * Google Maps API Version 3.0
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 * @link     http://code.google.com/apis/maps/
 * @link     http://maps.google.com
 */

/**
 * Global-scope map variable.
 */
var map;

/**
 * Global-scope variable for the marker image.
 */
var image;

/**
 * Global-scope variable for the marker drop shadow.
 */
var shadow;

/**
 * Global-scope variable for the marker area.
 */
var shape;

/**
 * Function to initialize the map.
 *
 * Code adapted from the Google API page.  Loads a map zoomed out all of
 * the way centered at latitude 0, longitude 0 (equator / prime meridian).
 * Populates the variables for the marker image, drop shadow, and shape of the
 * custom pin icons.
 *
 * @param string relativePath The relative path to the www folder.
 *
 * @access public
 * @return null
 */
function initialize(relativePath)
{
    var latlng = new google.maps.LatLng(0, 0);
    var myOptions = {
        zoom: 1,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById('map_canvas'),
        myOptions);
    image = new google.maps.MarkerImage(relativePath + 'images/map/marker.png',
        new google.maps.Size(26, 58),
        new google.maps.Point(0,0),
        new google.maps.Point(10, 58)
    );
    shadow = new google.maps.MarkerImage(relativePath + 'images/map/shadow.png',
        new google.maps.Size(45, 58),
        new google.maps.Point(0,0),
        new google.maps.Point(10, 58)
    );
    shape = {
        coord: [6, 57, 7, 20, 0, 14, 0, 0, 24, 3, 24, 10, 13, 20, 13, 57, 
            6, 57],
        type: 'poly'
    };
}

/**
 * Function to re-center the map at a different latitude and longitude.
 *
 * @param float lat The latitude to use.
 * @param float lng The longitude to use.
 *
 * @access public
 * @return null
 */
function centerMap(lat, lng)
{
    var latlng = new google.maps.LatLng(lat, lng);
    map.setCenter(latlng);
}

/**
 * Function to change the zoom level of the map by providing bounding 
 * coordinates.
 *
 * @param float lowerLat The lower left latitude value.
 * @param float lowerLng The lower left longitude value.
 * @param float upperLat The upper right latitude value.
 * @param float upperLng The upper right longitude value.
 *
 * @access public
 * @return null
 */
function zoomMap(lowerLat, lowerLng, upperLat, upperLng)
{
    var lowerLatLng = new google.maps.LatLng(lowerLat, lowerLng);
    var upperLatLng = new google.maps.LatLng(upperLat, upperLng);
    var latLngBounds = new google.maps.LatLngBounds(lowerLatLng, upperLatLng);
    map.fitBounds(latLngBounds);
}

/**
 * Function to add a new store to the map.
 *
 * @param float  lat     The latitude at which to place the marker.
 * @param float  lng     The longitude at which to place the marker.
 * @param string title   The marker's title.
 * @param string content The content of the infoWindow (address/etc). 
 */
function addStore(lat, lng, title, content)
{
    /**
     * Creates a new marker and attaches it to the map.
     */
    var latlng = new google.maps.LatLng(lat, lng);
    var marker = new google.maps.Marker({
        position: latlng, 
        map: map, 
        shadow: shadow,
        icon: image,
        shape: shape,
        title: title
    });
    
    /**
     * Creates a new infoWindow and attaches it to the map.
     */
    var infoWindow = new google.maps.InfoWindow({
        content: content,
        maxWidth: 300
    });
    
    /**
     * Creates the click event handler to display the infoWindow when clicked.
     */
    google.maps.event.addListener(marker, 'click', function() {
        infoWindow.open(map, marker);
    });
}
